package ig.da.abs;

import java.io.File;
import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Properties;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;

public class DataSourceUtil {
	
	public static java.sql.Connection getConnection(String param) throws SQLException, ClassNotFoundException, Exception {
            /*try {
                String JNDI_DS = param.trim().toUpperCase();
                Context ctx  = new InitialContext();
                DataSource ds = (DataSource)ctx.lookup( "java:/" + JNDI_DS );
                return ds.getConnection();
            } catch (Exception e) {
                //System.out.println(" DataSource no encontrado : "+ param.trim().toUpperCase() +":"+e.toString());
                //throw e;
            }*/
            
            FileInputStream fis = null;
            try {
                Properties properties = new Properties();
                properties.load(fis = new FileInputStream(new File("server/"+param+"-DS.properties")));
                Class.forName(properties.getProperty("driver"));
                return DriverManager.getConnection(properties.getProperty("url"), properties);    
            } catch (Exception e) {
                System.out.println(" DataSource no encontrado : "+ param.trim().toUpperCase() +":"+e.toString());
                throw e;
            }finally{
                try { fis.close(); } catch (Exception e) {}
            }
	}
}
